home *** CD-ROM | disk | FTP | other *** search
/ NeXT Education Software Sampler 1992 Fall / NeXT Education Software Sampler 1992 Fall.iso / Programming / Source / NLoad / LoadServer.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-08  |  3.7 KB  |  118 lines

  1. /*---------------------------------------------------------------------------
  2. LoadServer.c -- Supply load information to remote clients.
  3.  
  4. This server routine supplies load information to remote clients.  It was
  5. written on an Encore Multimax system running UMAX 4.3 BSD.  The need for this
  6. program is due to the lack of RPC support on the Encore.  This dedicated
  7. server supplies load information to remote NeXT clients running the NLoad
  8. program.  Systems that have Sun's RPC support do not need to run this
  9. dedicated server since the NeXT NLoad program supports RPC too.
  10.  
  11. This implementation is based on a UDP connectionless socket.  Loss of a
  12. packet here and there is of little concern.  This style of implementation
  13. results in minimal network traffic.
  14.  
  15. LoadServer sits patiently on a socket until a remote host sends a tickler
  16. packet.  It then returns a UDP packet containing the following string:
  17.  
  18.                  "serverHostname load1 load5 load15 scale"
  19.  
  20. Rex Pruess <rpruess@umaxc.weeg.uiowa.edu>
  21. -----------------------------------------------------------------------------*/
  22. #include    <stdio.h>
  23. #include    <sys/types.h>
  24. #include    <sys/socket.h>
  25. #include    <netinet/in.h>
  26. #include    <arpa/inet.h>
  27.  
  28. #define FAILURE -1
  29. #define QUEUES 3
  30. #define    UDPSERVERPORT 5500    /* Client must agree with this too! */
  31.  
  32. int loadave (char * buf);
  33.  
  34. main(argc, argv)
  35. int argc;
  36. char *argv[];
  37. {
  38.    char    buf[BUFSIZ];
  39.    int clientLength;
  40.    long vector[QUEUES];
  41.    int i;
  42.    int n;
  43.    int scale;
  44.    int sockfd;
  45.    char temp[16];
  46.  
  47.    struct sockaddr_in serverAddr;
  48.    struct sockaddr clientAddr;
  49.     
  50.    /* 
  51.     * Open a UDP socket (an Internet datagram socket).
  52.     */
  53.    if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
  54.       errExit (argv[0], "Can not open datagram socket.");
  55.  
  56.    /*
  57.     * Bind our local address so that the client can send to us.
  58.     */
  59.    bzero((char *) &serverAddr, sizeof(serverAddr));
  60.    serverAddr.sin_family = AF_INET;
  61.    serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
  62.    serverAddr.sin_port = htons(UDPSERVERPORT);
  63.  
  64.    if (bind(sockfd, (struct sockaddr *) &serverAddr, sizeof(serverAddr)) < 0)
  65.       errExit(argv[0], "Can not bind local address");
  66.  
  67.    /*
  68.     * Sit in this infinite loop waiting for a remote client to send a
  69.     * tickler packet.  Then, collect the load averages and return a packet
  70.     * to the client.
  71.     */
  72.    while (1)
  73.    {
  74.       clientLength = sizeof (clientAddr);
  75.       n = recvfrom(sockfd, buf, BUFSIZ, 0, (struct sockaddr *) &clientAddr, &clientLength);
  76.  
  77.       gethostname (buf, BUFSIZ - 2);
  78.       buf[BUFSIZ - 1] = '\0';
  79.         
  80.       if (loadave (buf + strlen(buf)) == FAILURE) {
  81.      fprintf (stderr, "%s: loadave subroutine failure.  Execution continues.\n", argv[0]);
  82.      continue;
  83.       }
  84.       
  85.       n = strlen(buf) + 1;
  86.       if (sendto(sockfd, buf, n, 0, (struct sockaddr *) &clientAddr, clientLength) != n)
  87.      fprintf (stderr, "%s: sento error.  Execution continues.\n", argv[0]);
  88.    }
  89.  
  90.    /* This line is never reached. */
  91. }
  92.  
  93. errExit (char *progName, char *errMsg)
  94. {
  95.    fprintf(stderr, "%s: %s\n", progName, errMsg);
  96.    exit(1);
  97. }
  98.  
  99.  
  100. /*---------------------------------------------------------------------------
  101. The code to calculate load averages is unique to each vendor.  On 4.3 BSD
  102. systems, refer to the source code for the "w" command.  The "w.c" source code
  103. is copyrighted by the Regents of the University of California.  Encore
  104. Computer Corporation has further copyrighted its version of "w.c".  Due to
  105. these copyrights, the code to calculate the load values is not included.
  106. -----------------------------------------------------------------------------*/
  107. int loadave (char * buf) {
  108.    int sumA, sumB, sumC, scale;
  109.  
  110.    /*
  111.     * Replace with code to calculate the load averages for your host.
  112.     */
  113.  
  114.    sprintf(buf, " %d %d %d %d\n", sumA, sumB, sumC, scale);
  115.    return 0;
  116. }
  117.  
  118.